/**
 * 格式化日期
 */
const formatTime = date => {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const hour = date.getHours()
    const minute = date.getMinutes()
    const second = date.getSeconds()

    return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

/**
 * 格式化年月日
 */
const formatTime2Date = date => {
    const year = date.getFullYear()
    const month = date.getMonth() + 1
    const day = date.getDate()

    return [year, month, day].map(formatNumber).join('/')
}

/**
 * 当前月份和日
 */
const formatWeek = () => {
    let date = new Date()
    const month = date.getMonth() + 1
    const day = date.getDate()
    const arr = ['今天', [month, day + 1].map(formatNumber).join('.'), [month, day + 2].map(formatNumber).join('.'), [month, day + 3].map(formatNumber).join('.'), [month, day + 4].map(formatNumber).join('.'), [month, day + 5].map(formatNumber).join('.'), [month, day + 6].map(formatNumber).join('.')]

    return arr
}

/**
 * 补零
 */
const formatNumber = n => {
    n = n.toString()
    return n[1] ? n : '0' + n
}

/**
 * 秒转时分秒
 */
const secondToDate = result => {
    let h = Math.floor(result / 3600) < 10 ? '0' + Math.floor(result / 3600) : Math.floor(result / 3600),
        m = Math.floor((result / 60 % 60)) < 10 ? '0' + Math.floor((result / 60 % 60)) : Math.floor((result / 60 % 60)),
        s = Math.floor((result % 60)) < 10 ? '0' + Math.floor((result % 60)) : Math.floor((result % 60))
    return result = m + ":" + s;
}

/**
 * 短链转长链
 */

const relative2absolute = url => {
    const base = 'https://sp.scdyfn.com'
    return base + url
}

module.exports = {
    formatTime,
    formatTime2Date,
    formatWeek,
    secondToDate,
    relative2absolute,
}